<?php

namespace App\Services;
//基础service  一些公共的方法的基类
use Illuminate\Support\Facades\Storage;
use zgldh\QiniuStorage\QiniuStorage;

class CommonService
{
    //算法
    //成功  返回数据
    public function success($data=[]){
        return  response()->json([
            'code'=>'200',
            'message'=>config('errorcode.code')[200],
            'data'=>$data,
        ]);
    }


    //失败   返回值、
    public function fail($code,$data=[]){
        return response()->json([
            'code'=>$code,
            'message'=>config('errorcode.code')[$code],
            'data'=>$data,
        ]);
    }
    //上传的照片
    public function UploadImg($request)
    {
        $ImgStr="";
        //判断有没有上传文件
        if ($request->hasFile('file')) {
            //接受文件名
            $file = $request->file('file');
            $disk = QiniuStorage::disk('qiniu');
            //文件名字
                if($file->getClientSize()<20971520)
                {
                    $fileName = md5($file->getClientOriginalName().time().rand()).'.'.$file->getClientOriginalExtension();
                    $bool = $disk->put('iwanli/image_'.$fileName,file_get_contents($file->getRealPath()));
                    //上传成功
                    if ($bool) {
                        $ImgStr ="http://pngjvz1fx.bkt.clouddn.com/iwanli/image_".$fileName;
                    }
                }
            }
            return $ImgStr;
    }

    //token验证
    public function CheckToken($token)
    {
        if($token!=='')
        {
            $redis=new \redis();
            $redis->connect("127.0.0.1",6379);
            if($userInfo=$redis->get($token))
            {

                $userInfo=json_decode($userInfo,true);
                $oldtoken=$redis->get($userInfo[0]['u_phone']);
                if($oldtoken==$token)
                {
                    return $this->success($userInfo);
                }
                else
                {
                    return $this->fail(215,"token无效");
                }
            }
            else
            {
                return $this->fail(215,"token无效");
            }
        }
        else
        {
            return $this->fail(215,"token无效");
        }
    }


    /**
     * 发送验证码
     */
    public function SendCodes($tel){

        //开启redis
        $redis=new \redis();
        $redis->connect("127.0.0.1",6379);
        //随机数
        $tmps=floor(rand(100000,999999));
        if($redis->get($tel)){
            $redis->delete($tel);
            //放入缓存中  并设置过期时间  五分钟
            $redis->set($tel,$tmps);
            $redis->expire($tel,60*5);
        }else{
            //放入缓存中  并设置过期时间  五分钟
            $redis->set($tel,$tmps);
            $redis->expire($tel,60*5);
        }


        $smsapi = "http://api.smsbao.com/";
        $user = "ljnkb"; //短信平台帐号
        $pass = md5("Lijing06161106--"); //短信平台密码
        $content="您的验证码是".$tmps."   五分钟后失效哦";//要发送的短信内容
//        return $content;

        $phone =  $tel;//要发送短信的手机号码
        $sendurl = $smsapi."sms?u=".$user."&p=".$pass."&m=".$phone."&c=".urlencode($content);
        $result =file_get_contents($sendurl) ;


//        echo $result;
//        return 200;
        if($result=='0'){
            return  200;
        }else{
            return $result;

        }

    }




}
